package com.muk.meis.controller.login;

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.muk.meis.MEISConstance;
import com.muk.meis.model.bean.UserProfile;
import com.muk.meis.model.service.UserService;
import com.muk.webframework.framework.controller.BaseController;

/**
 * Servlet implementation class LoginController
 */
@WebServlet("/LoginController/*")
public class LoginController extends BaseController {
	private static final long serialVersionUID = -1394869395612800855L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginController() {
		super();
	}

	/**
	 * @throws IOException 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	public void login() throws IOException {
		String userName = request.getParameter("username");
		String passWord = request.getParameter("password");
		System.out.println("user name " + userName + " password " + passWord);
		UserService service = new UserService();
		if (service.loginUser(userName, passWord)) {
			UserProfile userProfile = service.getUserPassLoginDetail();			
			session.setAttribute(MEISConstance.USER_PASS_LOGIN, userProfile);
			if(MEISConstance.ADMIN_ROLE.equals(userProfile.getRole())) {
				response.sendRedirect(request.getContextPath());
			} else {
				response.sendRedirect(request.getContextPath()+"/school/listnews.jsp?percode="+userProfile.getPercodeCode());
			}
		} else {
			response.sendRedirect(request.getContextPath()+"/index.jsp");
		}
	}
	
	public void logout() throws IOException {
		session.removeAttribute(MEISConstance.USER_PASS_LOGIN);
		response.sendRedirect(request.getContextPath());
	}
	
//	protected void doGet(HttpServletRequest request,
//			HttpServletResponse response) throws ServletException, IOException {
//		HttpSession session = request.getSession();
//		String userName = request.getParameter("username");
//		String passWord = request.getParameter("password");
//		System.out.println("user name " + userName + " password " + passWord);
//		UserService service = new UserService();
//		if (service.loginUser(userName, passWord)) {
//			session.setAttribute(MEISConstance.USER_PASS_LOGIN, service.getUserPassLoginDetail());
//			response.sendRedirect("");
//		}
//	}

}
